<template>
  <el-container class="homeContainer">
    <!-- 头部区域 -->
    <el-header>
      <div class="headerLf">
        <img src="../assets/heima.png" alt="logo">
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>

    <!-- 主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse? '64px' : '200px'">
        <div class="toggleButton" @click="toggleCollapse">|||</div>
        <el-menu background-color="#b3c0d1" text-color="#fff" active-text-color="lightblue"
          :unique-opened="true" :collapse="isCollapse"
          :collapse-transition="false" router :default-active="menuStatus">
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + '' " v-for="item in asideMenuList" :key="item.id">
             <!-- 一级菜单模板（图标和文本） -->
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span class="spanFont">{{item.authName}}</span>
            </template>
             <!-- 二级菜单 -->
            <el-menu-item :index="'/' + submenu.path" v-for="submenu in item.children" :key="submenu.id" @click="changeMenuStatus('/' + submenu.path)">
              <!-- 二级菜单模板（图标和文本） -->
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{submenu.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容区域 -->
      <el-main>
        <router-view/>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data(){
    return {
      asideMenuList:[],  // 左侧菜单数据数组
      iconsObj:{
        '125': 'iconfont icon-user',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      isCollapse:false,  //是否展开和折叠菜单
      menuStatus: ''
    }
  },
  created() {
    // 获取左侧菜单
    this.getAside(),
    this.menuStatus =  window.sessionStorage.getItem('menuStatus')
  },
  methods: {
    logout () {
      //清除token并跳转到登录页
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取左侧菜单
    async getAside(){
      const { data: res } = await this.$http.get('/menus')
      if(res.meta.status !== 200) return res.meta.msg
      this.asideMenuList = res.data
      console.log(this.asideMenuList)
    },
    // 点击展开或者折叠菜单
    toggleCollapse(){
      this.isCollapse = ! this.isCollapse
    },
    // 切换子菜单回调
    changeMenuStatus(menuStatus){
      window.sessionStorage.setItem('menuStatus',menuStatus)
      this.menuStatus = menuStatus
    }
  }
}
</script>
<style lang="less" scoped>
.el-header {
  background-color: #b3c0d1;
  color: #333;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 5px;
  > .headerLf {
    display: flex;
    align-items: center;
    > img {
      margin-right: 10px;
    }
  }
}

.el-aside {
  background-color: #d3dce6;
  color: #333;
  .el-menu {
    border-right: 0;
  }
}

.el-main {
  background-color: #e9eef3;
  color: #333;
}
.homeContainer {
  height: 100%;
}
// 一级图标与右侧字体之间的间距
.spanFont{
    margin-left: 10px;
}
// 折叠和展开菜单
.toggleButton{
  text-align: center;
  font-size: 15px;
  background: lightsteelblue;
  line-height: 30px;
  letter-spacing: 5px;
  color: #fff;
  cursor: pointer;
}
</style>
